package com.jzy.gulimall.cart.controller;

import com.jzy.common.utils.R;
import com.jzy.gulimall.cart.service.CartService;
import com.jzy.gulimall.cart.vo.CartItemVo;
import com.jzy.gulimall.cart.vo.CartVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author jzy
 * @date 2020/9/18 12:32
 */
@Controller
public class CartController {

    @Resource
    private CartService cartService;

    /**
     * 订单确认页
     * 获取用户所有选中购物项
     * 远程调用丢失请求头
     */
    @GetMapping("/getUserCartItem")
    @ResponseBody
    public List<CartItemVo> getUserCartItem() {
        return cartService.getUserCartItem();
    }


    /**
     *  购物车 删除购物项
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     *  购物车 修改数量 +1 -1
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {
        cartService.countItem(skuId, num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     *  购物车 勾选中购物车
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("check") Integer check) {
        cartService.checkItem(skuId, check);
        return "redirect:http://cart.gulimall.com/cart.html";

    }

    /**
     * 登录时 合并购物车
     */
    @PostMapping("/mergeCart")
    @ResponseBody
    public R mergeCart(@RequestParam("userKey") String userKey, @RequestParam("userId") Long userId) {
        try {
            cartService.mergeCart(userKey, userId);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }


    /**
     * 购物车 去购物车页面
     * 判断是否登录
     * 浏览器保存一个cookie:user-key:表示用户身份,一个月后过期
     * 每次访问带user-key,没有临时用户,则创建一个临死用户
     * 返回所有购物车数据
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) {
        CartVo cartVo = cartService.getCart();
        model.addAttribute("cart", cartVo);
        return "cartList";
    }

    /**
     * 购物车 添加商品
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes attributes) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId, num);
        attributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccessPage.html";
    }

    /**
     * 购物车 添加商品 去成功页面
     */
    @GetMapping("/addToCartSuccessPage.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId, Model model) {
        CartItemVo cartItemVo = cartService.getCartItem(skuId);
        model.addAttribute("cartItem", cartItemVo);
        return "success";
    }
}
